<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-touch-fullscreen" content="yes">
  <meta name="msapplication-tap-highlight" content="no">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <title>修改密码</title>
  <link rel="shortcut icon" href="/favicon.ico">
  <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/bootstrap-4.2.1/css/bootstrap.min.css">
  <link rel="stylesheet" href="${pageContext.request.contextPath }/user/SUI_Mobile/sm.min.css">
  <link rel="stylesheet" href="${pageContext.request.contextPath }/user/SUI_Mobile/sm-extend.min.css">
  <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/user/css/common.css">
  
</head>
<body>
  <div class="container">
    <header class="bar bar-nav header sticky-top">
      <a class="button button-link button-nav pull-left back" href="#" onclick="history.go(-1)">
        <span class="icon icon-left"></span>返回
      </a>
      <h1 class='title'>修改密码</h1>
    </header>
    <!-- 中间，记录显示 -->
    <div class="mainContent">
      <form class="list-block" id="editPasswordForm">
        <input type="text" class="uid" value="${user.uid }" style="display:none;">
        <input type="password" class="password" value="${user.password }" style="display:none;">
        <div class="text-center updateErrorInfo error" style="color:#f00;display:none;padding: 10px 0px;">原密码错误</div>
        <ul>
          <li>
            <div class="item-content">
              <div class="item-inner">
                <div class="label">原密码</div>
                <div class="item-input">
                  <input type="password" value="" name="oldPassword" class="oldPassword">
                </div>
              </div>
            </div>
            <label id="oldPassword-error" class="error" for="oldPassword">这是必填字段</label>
          </li>
          <li>
            <div class="item-content">
              <div class="item-inner">
                <div class="label">新密码</div>
                <div class="item-input">
                  <input type="password" value="" name="newPassword" class="newPassword" id="newPassword">
                </div>
              </div>
            </div>
            <label id="newPassword-error" class="error" for="newPassword">这是必填字段</label>
          </li>
          <li>
            <div class="item-content">
              <div class="item-inner" style="border-bottom: 0.5px solid #E7E7E7;margin-bottom: 5px;">
                <div class="label">确认密码</div>
                <div class="item-input">
                  <input type="password" value="" name="confirmNewPassword" class="confirmNewPassword">
                </div>
              </div>
            </div>
            <label id="confirmNewPassword-error" class="error" for="confirmNewPassword">两次输入密码不一致</label>
          </li>
        </ul>
      </form>
      <div class="content-block">
        <div class="row">
          <div class="col"><a href="javascript:;" class="button button-big button-fill button-success confirmUpdatePwdBtn">确定</a></div>
        </div>
      </div>
      <div class="modal toast modal-out" style="display:block;margin-top:-24px;margin-left:-48px;"></div>
    </div><!-- end of .mainContent -->
  </div> <!-- end of .container -->

  <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-3.3.1.min.js" charset="utf-8"></script>
  <script type='text/javascript' src='${pageContext.request.contextPath }/user/SUI_Mobile/zepto.min.js' charset='utf-8'></script>
  <script type='text/javascript' src='${pageContext.request.contextPath }/user/SUI_Mobile/sm-extend.min.js' charset='utf-8'></script>
  <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery.validate.min.js"></script>
  <script type="text/javascript" src="${pageContext.request.contextPath }/js/messages_zh.js"></script>
  <script type="text/javascript" src="${pageContext.request.contextPath }/bootstrap-4.2.1/js/bootstrap.min.js" charset="utf-8"></script>
  <script>
    $(function () {
      jQuery.validator.setDefaults({
    	  success:'valid'
      })
      $('#editPasswordForm').validate({
    	  rules:{
    		  oldPassword:{
    			  required:true,
    		  },
    		  newPassword:{
    			  required:true,
    			  minlength:6
    		  },
    		  confirmNewPassword:{
    			  equalTo:newPassword
    		  }
    	  },
    	  messages:{
    		  confirmNewPassword:{
    			  equalTo:"两次输入密码不一致"
    		  }
    	  }
      })
      // 点击 确定， 修改密码
      $('.confirmUpdatePwdBtn').on('click', function(){
    	  if($('#editPasswordForm').valid()){
    		  updatePassword()
    	  }
      })
    })
    // 修改密码
    function updatePassword(){
    	var uid = $('.uid').val()
    	var password = $('.password').val()
    	var oldPassword = $('.oldPassword').val()
    	var newPassword = $('.newPassword').val()
    	var confirmNewPassword = $('.confrimNewPassword').val()
    	if(oldPassword!=password){
    		$('.updateErrorInfo').css('display','block')
    		$('.newPassword').val('')
    		$('.confirmNewPassword').val('')
    	}else {
	    	$.post(
	    		"/LostAndFound/me?method=updatePassword",
	    		{
	    			"uid":uid,
	    			"newPassword":newPassword,
	    		},
	    		function(data){
	    			showToastByTime(data.success)
	    		},
	    		"json"
	    	);
    	}
    }
    // 显示提示，然后跳转页面
    function showToastByTime ( successFlag ) {
      var info = null;
      if( successFlag ){
        info = '修改成功'
      }else {
        info = '修改失败'
      }
      clearTimeout(timer)
      $('.toast').text(info)    
      $('.toast').addClass('modal-in')
      $('.toast').removeClass('modal-out')
      var timer = window.setTimeout(function () {
        $('.toast').addClass('modal-out')
        $('.toast').removeClass('modal-int')
        location.reload();
      }, 1200)
      
    }
  </script>

</body>
</html>